<?php
namespace app\index\controller;

use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;

use Symfony\Component\DomCrawler\Crawler;//html节点处理
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;//guzzlehttp处理框架
/**
 * jd详情数据抓取
 */
class Jd extends Front
{
    public function index()
    {

        $this->view(); //读取一片文章详情
        die;
    }
    //获取文章详情
    public function view($url='https://item.jd.com/46082103667.html')
    {

        //$url = 'https://www.163.com/renjian/article/GD3JI9NE000181RK.html';
        //163随意一片文章
        //header("Content-type: text/html; charset=gbk");
        echo '目标地址：'.$url.'<br>';
        $promise = $this->sendRequest($url);
        //设置回调
        $promise->then(
            function (ResponseInterface $res) {
                echo '1';
                // $res->getStatusCode() . "\n";
                $html = (String)$res->getBody();
                //echo $html;
                //$html = iconv("UTF-8", "GBK", $html); 
                //$html = mb_convert_encoding($html, "UTF-8", "GBK");
                //preg_match_all('/window.location.replace\(\"(\w+_*.*)*\"\)/',$html,$match,PREG_PATTERN_ORDER);
                //var_dump($match);
                $crawler = new Crawler($html);
                $title = $crawler->filterXPath('descendant-or-self::title')->first();
                echo '标题:'.$title->text().'<br>';
                
                echo '<span onclick="CopyUrl()">复制</span>';
                echo '<script type="text/javascript">
           function CopyUrl() {
                    let url = document.querySelector("#copyObj");
                    url.select(); // 选择对象
                    document.execCommand("Copy");
             }
</script>';
                
                $con = '';
                $crawler->filterXPath('descendant-or-self::p')->each(function($node,$i)use(&$con){
                    $con.='<p>'.$node->html().'</p>';
                });
                echo $con;
                echo '<textarea id="copyObj" style="opacity: 0;">';
                echo $con;
                echo '</textarea>';
            },
            function (RequestException $e) {
                echo '2';
                echo $e->getMessage() . "\n";
                echo $e->getRequest()->getMethod();
            }
        );
        //等待响应
        $promise->wait();
    }
    protected function sendRequest($url)
    {

        $jar = new \GuzzleHttp\Cookie\CookieJar;
        $client = new \GuzzleHttp\Client(['verify' =>false,'cookies'=>true]);//关闭安全验证
        $promise = $client->requestAsync('GET',$url,[
        'allow_redirects' => false,
        'cookies' => $jar,
        'headers' => [
            'Accept-Encoding' => 'gzip, deflate, br',
            'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Language' => 'zh-CN,zh;q=0.9,en;q=0.8',
            'Cache-Control'   => 'no-cache',
            'Connection'      => 'keep-alive',
            'User-Agent'      => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
            ]
        ]);//创建请求
        return $promise;
    }
}
